package com.ssmk.module.shoppingcar.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.LogUtils;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.ssmk.R;
import com.ssmk.module.shoppingcar.bean.OrderAllBean;
import com.ssmk.module.shoppingcar.bean.SkuInfoBean;
import com.ssmk.module.shoppingcar.contract.AllOrderContract;
import com.ssmk.module.shoppingcar.order.EvaluateActivity;
import com.ssmk.module.shoppingcar.order.LogisticsDetailsActivity;
import com.ssmk.utlis.PopUtils;

import java.util.List;

/**
 * author：郑志强
 * data：2017/7/6 19:19
 * function：
 */

public class AllOrderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private final List<OrderAllBean.DataBean> list;
    private Context mContext;
    private OrderAllBean cancelDeleteOrderBean;
    private AllOrderContract.OnOrderButtonListener onOrderButtonListener;

    public AllOrderAdapter(Context context, List<OrderAllBean.DataBean> datas) {
        this.mContext = context;
        this.list = datas;
    }
    public void setOnOrderButtonListener(AllOrderContract.OnOrderButtonListener onOrderButtonListener) {
        this.onOrderButtonListener = onOrderButtonListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.allorder_recycler_item, parent, false);
        OrderHolder holder = new OrderHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);

        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        Gson gson = new Gson();
        if (holder instanceof OrderHolder) {
            final OrderHolder holders = (OrderHolder) holder;
            List<OrderAllBean.DataBean.OrderInfoDetailListBean> detailList = list.get(position).getOrderInfoDetailList();
            for (int i = 0; i < detailList.size(); i++) {
                String skuInfo =detailList.get(i).getSkuInfo();
                SkuInfoBean skuInfoBean = gson.fromJson(skuInfo, SkuInfoBean.class);
                if (!TextUtils.isEmpty(skuInfoBean.getCommodityFormat().getGoodsFormatPhoto())){
                    Glide.with(mContext).load(skuInfoBean.getCommodityFormat().getGoodsFormatPhoto()).into(holders.allorder_shop_iv);
                }else {
                    //Toast.makeText(mContext,"图片地址为空",Toast.LENGTH_SHORT).show();
                }
                if (list.get(position).getOrderInfoDetailList().get(i).getGoodsName() != null) {
                    holders.allorder_shop_name.setText(list.get(position).getOrderInfoDetailList().get(i).getGoodsName());
                } else {
                    holders.allorder_shop_name.setText("");
                }
                holders.allorder_shop_count.setText(list.get(position).getOrderInfoDetailList().get(i).getPurchaseSize() + "");
                holders.goods_count.setText("共" + list.get(position).getOrderInfoDetailList().get(i).getPurchaseSize() + "件商品 需付款：");
                holders.allorder_shop_color.setText(skuInfoBean.getCommodityFormat().getGoodsFormatName());
                if (!TextUtils.isEmpty(list.get(position).getOrderInfoDetailList().get(i).getSizeNumberId())) {
                    holders.allorder_shop_size.setText(list.get(position).getOrderInfoDetailList().get(i).getSizeNumberId());//尺码
                } else {
                    holders.allorder_shop_size.setText("");//尺码
                }
            }

            if (!TextUtils.isEmpty(list.get(position).getOrderPrice())) {
                holders.allorder_shop_price.setText(list.get(position).getOrderPrice());//订单金额
            } else {
                holders.allorder_shop_price.setText("");//订单金额
            }
            /**
             * Adapter中的if...else一定要成对出现，不然if不成立的时候会复用上面的item的数据
             */

            if (Integer.valueOf(list.get(position).getOrderStatus()) == 0) {//未发货
                holders.allorder_shop_state.setText(R.string.waitSend);//待发货   订单状态

                holders.wait_send_check_btn.setVisibility(View.VISIBLE);
                holders.refound_btn.setVisibility(View.VISIBLE);
                holders.waitpayorder_btn.setVisibility(View.GONE);
                holders.waitpayorder_btn_cancel.setVisibility(View.GONE);
                holders.sure_receivegoods_btn.setVisibility(View.GONE);
                holders.delete_order_btn.setVisibility(View.GONE);
                holders.evaluate_btn.setVisibility(View.GONE);
                holders.delete_order_btn2.setVisibility(View.GONE);
                holders.checkout_order_details_btn.setVisibility(View.GONE);

            } else if (Integer.valueOf(list.get(position).getOrderStatus()) == 1) {
                holders.allorder_shop_state.setText(R.string.alreadySend);// 已发货   订单状态

                holders.checkout_order_details_btn.setVisibility(View.VISIBLE);
                holders.sure_receivegoods_btn.setVisibility(View.VISIBLE);
                holders.waitpayorder_btn.setVisibility(View.GONE);
                holders.waitpayorder_btn_cancel.setVisibility(View.GONE);
                holders.delete_order_btn.setVisibility(View.GONE);
                holders.evaluate_btn.setVisibility(View.GONE);
                holders.delete_order_btn2.setVisibility(View.GONE);
                holders.wait_send_check_btn.setVisibility(View.GONE);
                holders.refound_btn.setVisibility(View.GONE);

            } else if (Integer.valueOf(list.get(position).getOrderStatus()) == 4) {
                holders.allorder_shop_state.setText(R.string.dealComplate);//交易完成  订单状态
                holders.checkout_order_details_btn.setVisibility(View.GONE);
                holders.waitpayorder_btn.setVisibility(View.GONE);
                holders.waitpayorder_btn_cancel.setVisibility(View.GONE);
                holders.sure_receivegoods_btn.setVisibility(View.GONE);
                holders.delete_order_btn.setVisibility(View.VISIBLE);
                holders.evaluate_btn.setVisibility(View.VISIBLE);
                holders.delete_order_btn2.setVisibility(View.GONE);
                holders.wait_send_check_btn.setVisibility(View.GONE);
                holders.refound_btn.setVisibility(View.GONE);
            } else {
                holders.allorder_shop_state.setText(R.string.waitPay);//订单状态
                if (mContext.getString(R.string.meNotpay).equals(list.get(position).getFlag())
                        || mContext.getString(R.string.userInfoError).equals(list.get(position).getFlag())
                        || mContext.getString(R.string.otherReason).equals(list.get(position).getFlag())) {
                    holders.checkout_order_details_btn.setVisibility(View.GONE);
                    holders.waitpayorder_btn.setVisibility(View.GONE);
                    holders.waitpayorder_btn_cancel.setVisibility(View.GONE);
                    holders.sure_receivegoods_btn.setVisibility(View.GONE);
                    holders.delete_order_btn.setVisibility(View.GONE);
                    holders.evaluate_btn.setVisibility(View.GONE);
                    holders.delete_order_btn2.setVisibility(View.VISIBLE);
                    holders.wait_send_check_btn.setVisibility(View.GONE);
                    holders.refound_btn.setVisibility(View.GONE);
                } else {
                    holders.checkout_order_details_btn.setVisibility(View.GONE);
                    holders.waitpayorder_btn.setVisibility(View.VISIBLE);
                    holders.waitpayorder_btn_cancel.setVisibility(View.VISIBLE);
                    holders.sure_receivegoods_btn.setVisibility(View.GONE);
                    holders.delete_order_btn.setVisibility(View.GONE);
                    holders.evaluate_btn.setVisibility(View.GONE);
                    holders.delete_order_btn2.setVisibility(View.GONE);
                    holders.wait_send_check_btn.setVisibility(View.GONE);
                    holders.refound_btn.setVisibility(View.GONE);
                }
                //待支付----取消订单
                holders.waitpayorder_btn_cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (null != onOrderButtonListener) {
                            onOrderButtonListener.cancelOrder(list.get(position).getOrderId());
                        }
/*
                        View popupView = PopUtils.getPopViewCancel(mContext);
                        final PopupWindow popWin = PopUtils.getPopUpWindow(popupView);
                        final Activity mActivity = PopUtils.showPop(mContext,v,popWin);

                        final TextView NoPay = (TextView) popupView.findViewById(R.id.NoPay);
                        final TextView InfoError = (TextView) popupView.findViewById(R.id.InfoError);
                        final TextView OtherReason = (TextView) popupView.findViewById(R.id.OtherReason);
                        final TextView Cancel = (TextView) popupView.findViewById(R.id.Cancel);
                        //我不想买了
                        NoPay.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                //取消订单
                                AllOrderPresenter presenter = new AllOrderPresenter(AllOrderAdapter.this, mContext, list.get(position).getOrderId(), 0, (String) NoPay.getText());
                                presenter.GetDataModel();
                                popWin.dismiss();
                            }
                        });

                        //信息填写错误
                        InfoError.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                AllOrderPresenter presenter = new AllOrderPresenter(AllOrderAdapter.this, mContext, list.get(position).getOrderId(), 0, (String) InfoError.getText());
                                presenter.GetDataModel();  //取消订单
                                popWin.dismiss();
                            }
                        });
                        //其它原因
                        OtherReason.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                //取消订单
                                AllOrderPresenter presenter = new AllOrderPresenter(AllOrderAdapter.this, mContext, list.get(position).getOrderId(), 0, (String) OtherReason.getText());
                                presenter.GetDataModel();
                                popWin.dismiss();

                            }
                        });
                        //取消
                        Cancel.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                popWin.dismiss();
                            }
                        });

                        //popdimiss的监听
                        popWin.setOnDismissListener(new PopupWindow.OnDismissListener() {
                            @Override
                            public void onDismiss() {
                                //背景色还原
                                WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes();
                                lp.alpha = 1f;
                                mActivity.getWindow().setAttributes(lp);
                            }
                        });*/
                    }
                });
                //去支付
                holders.waitpayorder_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        View popView = PopUtils.getPopView(mContext);
                        PopupWindow pop = PopUtils.getPopUpWindow(popView);
                        PopUtils.showPop(mContext,v,pop);

                    }
                });
                //待收货-查看物流
                holders.checkout_order_details_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //跳转物流页面
                        Intent intent = new Intent(mContext,LogisticsDetailsActivity.class);
                        intent.putExtra("logisticsCode",(String)list.get(position).getLogisticsCode());//快递公司代码
                        intent.putExtra("courierNumber",(String)list.get(position).getCourierNumber());//运单号
                        mContext.startActivity(intent);
                    }
                });
                //待收货--确认收获
                holders.sure_receivegoods_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });
                //待发货--查看物流
                holders.wait_send_check_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });
                //待发货--退款
                holders.refound_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                    }
                });

                //交易完成-删除订单
                holders.delete_order_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (null != onOrderButtonListener) {
                            onOrderButtonListener.deleteOrder(list.get(position).getOrderId());
                        }

                       /* AllOrderPresenter presenter = new AllOrderPresenter(AllOrderAdapter.this, mContext, list.get(position).getOrderId(), 1, "");
                        presenter.GetDataModel();*/
                    }
                });
                //交易完成-评价
                holders.evaluate_btn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(mContext,EvaluateActivity.class);
                        mContext.startActivity(intent);
                    }
                });
                //删除订单
                holders.delete_order_btn2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (null != onOrderButtonListener) {
                            onOrderButtonListener.deleteOrder(list.get(position).getOrderId());
                        }

                        /*AllOrderPresenter presenter = new AllOrderPresenter(AllOrderAdapter.this, mContext, list.get(position).getOrderId(), 1, "");
                        presenter.GetDataModel();*/
                    }
                });
            }
        }
        holder.itemView.setTag(position);
    }

    //得到取消订单的结果
    public void getCancelDeleteDate(Object o, String orderId, String cancelOrderFlag) {
        if (o instanceof OrderAllBean) {
            cancelDeleteOrderBean = (OrderAllBean) o;
            if (cancelDeleteOrderBean.getCode() == 200) {
                Toast.makeText(mContext, cancelDeleteOrderBean.getMsg(), Toast.LENGTH_SHORT).show();
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getOrderId().equals(orderId)) {
                        list.get(i).setFlag(cancelOrderFlag);
                        LogUtils.e("取消订单的id",orderId);
                        break;
                    }
                }
                this.notifyDataSetChanged();
            } else {
                //TODO 删除失败的时候可以给用户一些提示
                Toast.makeText(mContext, cancelDeleteOrderBean.getMsg(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    //得到删除订单的结果
    public void getDeleteDate(Object o, String orderId) {
        if (o instanceof OrderAllBean) {
            OrderAllBean deleteOrderBean = (OrderAllBean) o;
            if (deleteOrderBean.getCode() == 200) {
                Toast.makeText(mContext, deleteOrderBean.getMsg(), Toast.LENGTH_SHORT).show();
                //确保取消的是用户点击的订单
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getOrderId().equals(orderId)) {
                        list.remove(i);
                        LogUtils.e("删除订单的id",orderId);
                        break;
                    }
                }
                notifyDataSetChanged();
            } else {
                //TODO 删除失败的时候可以给用户一些提示
                Toast.makeText(mContext, cancelDeleteOrderBean.getMsg(), Toast.LENGTH_SHORT).show();
            }
        }
    }


    @Override
    public int getItemCount() {
        if (null == list)
            return 0;
        return list.size();
    }

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(view, (int) view.getTag());
        }
    }

    class OrderHolder extends RecyclerView.ViewHolder {
        private final ImageView allorder_shop_iv;
        private final TextView allorder_shop_state;
        private final TextView allorder_shop_name;
        private final TextView allorder_shop_count;
        private final TextView goods_count;
        private final TextView allorder_shop_size;
        private final TextView allorder_shop_color;
        private final TextView allorder_shop_price;
        private final Button waitpayorder_btn;
        private final Button waitpayorder_btn_cancel;
        private final Button checkout_order_details_btn;
        private final Button sure_receivegoods_btn;
        private final Button delete_order_btn;
        private final Button evaluate_btn;
        private final Button delete_order_btn2;
        private final Button refound_btn;
        private final Button wait_send_check_btn;


        public OrderHolder(View itemView) {
            super(itemView);
            allorder_shop_iv = (ImageView) itemView.findViewById(R.id.allorder_shop_iv);
            allorder_shop_state = (TextView) itemView.findViewById(R.id.allorder_shop_state);
            allorder_shop_name = (TextView) itemView.findViewById(R.id.allorder_shop_name);
            allorder_shop_count = (TextView) itemView.findViewById(R.id.allorder_shop_count);
            goods_count = (TextView) itemView.findViewById(R.id.goods_count);
            allorder_shop_size = (TextView) itemView.findViewById(R.id.allorder_shop_size);
            allorder_shop_color = (TextView) itemView.findViewById(R.id.allorder_shop_color);//商品颜色
            allorder_shop_price = (TextView) itemView.findViewById(R.id.allorder_shop_price);
            //待支付-去支付
            waitpayorder_btn = (Button) itemView.findViewById(R.id.waitpayorder_btn);
            //待支付-取消
            waitpayorder_btn_cancel = (Button) itemView.findViewById(R.id.waitpayorder_btn_cancel);
            //退款
            wait_send_check_btn = (Button) itemView.findViewById(R.id.wait_send_check_btn);
            refound_btn = (Button) itemView.findViewById(R.id.refound_btn);
            //查看物流
            checkout_order_details_btn = (Button) itemView.findViewById(R.id.checkout_order_details_btn);
            //已发货-确认收获
            sure_receivegoods_btn = (Button) itemView.findViewById(R.id.sure_receivegoods_btn);
            //交易完成-删除订单
            delete_order_btn = (Button) itemView.findViewById(R.id.delete_order_btn);
            //交易完成-评价
            evaluate_btn = (Button) itemView.findViewById(R.id.evaluate_btn);
            //订单取消成功，删除订单
            delete_order_btn2 = (Button) itemView.findViewById(R.id.delete_order_btn2);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
    private OnItemClickListener mOnItemClickListener = null;
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}

